package cn.hahah.module.blog.controller.admin.userreply;

import cn.hahah.module.blog.dal.dataobject.usercomment.UserCommentDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.usercomment.UserCommentService;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userreply.vo.*;
import cn.hahah.module.blog.dal.dataobject.userreply.UserReplyDO;
import cn.hahah.module.blog.service.userreply.UserReplyService;

@Tag(name = "管理后台 - 用户回复评论")
@RestController
@RequestMapping("/blog/user-reply")
@Validated
public class UserReplyController {

    @Resource
    private UserReplyService userReplyService;
    @Resource
    private UsersService usersService;
    @Resource
    private UserCommentService userCommentService;

    @PostMapping("/create")
    @Operation(summary = "创建用户回复评论")
    @PreAuthorize("@ss.hasPermission('blog:user-reply:create')")
    public CommonResult<Long> createUserReply(@Valid @RequestBody UserReplySaveReqVO createReqVO) {
        return success(userReplyService.createUserReply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户回复评论")
    @PreAuthorize("@ss.hasPermission('blog:user-reply:update')")
    public CommonResult<Boolean> updateUserReply(@Valid @RequestBody UserReplySaveReqVO updateReqVO) {
        userReplyService.updateUserReply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户回复评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-reply:delete')")
    public CommonResult<Boolean> deleteUserReply(@RequestParam("id") Long id) {
        userReplyService.deleteUserReply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户回复评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-reply:query')")
    public CommonResult<UserReplyRespVO> getUserReply(@RequestParam("id") Long id) {
        UserReplyDO userReply = userReplyService.getUserReply(id);
        return success(BeanUtils.toBean(userReply, UserReplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户回复评论分页")
    @PreAuthorize("@ss.hasPermission('blog:user-reply:query')")
    public CommonResult<PageResult<UserReplyRespVO>> getUserReplyPage(@Valid UserReplyPageReqVO pageReqVO) {
        PageResult<UserReplyDO> pageResult = userReplyService.getUserReplyPage(pageReqVO);
        PageResult<UserReplyRespVO> result = BeanUtils.toBean(pageResult, UserReplyRespVO.class);
        for (UserReplyRespVO userReplyRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userReplyRespVO.getUserId());
            UserCommentDO userCommentDO = userCommentService.getUserComment(userReplyRespVO.getCommentId());

            if( usersDO!=null ){
                userReplyRespVO.setUserName(usersDO.getUsername());
            }
            if( userCommentDO!=null ){
                userReplyRespVO.setCommentName(userCommentDO.getContent());
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户回复评论 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-reply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserReplyExcel(@Valid UserReplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserReplyDO> list = userReplyService.getUserReplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户回复评论.xls", "数据", UserReplyRespVO.class,
                        BeanUtils.toBean(list, UserReplyRespVO.class));
    }

}